﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.Text;

/// <summary>
/// Server Page基类
/// </summary>
public class ServerPage : System.Web.UI.Page
{
    public ServerPage()
    {
        //
        // TODO: 在此处添加构造函数逻辑
        //
    }


    // ---------------------- 输出方法 ------------------- //
    /// <summary>
    /// 返回结果XML
    /// result：显示值    
    /// </summary>
    #region void OutputXml(string result)
    public void OutputXml(string result)
    {
        // ===================================================
        // write xml
        Response.Clear();
        Response.ContentType = "text/xml";
        Response.ContentEncoding = System.Text.Encoding.UTF8;
        Response.Expires = -1;
        Response.CacheControl = "no-cache";

        XmlTextWriter xmlWriter = new XmlTextWriter(Response.OutputStream, System.Text.Encoding.UTF8);
        xmlWriter.WriteStartDocument();

        xmlWriter.WriteStartElement("Root");
        xmlWriter.WriteElementString("Result", result);
        xmlWriter.WriteEndElement();

        xmlWriter.WriteEndDocument();

        xmlWriter.Flush();
        xmlWriter.Close();

        Response.Flush();
        Response.End();
    }
    #endregion

    /// <summary>
    /// 返回结果JSON
    /// result：显示值    
    /// </summary>
    #region void OutputJSon(string result)
    public void OutputJSon(string result)
    {
        // ===================================================
        // write xml
        Response.Clear();
        Response.ContentEncoding = System.Text.Encoding.UTF8;
        Response.Expires = -1;
        Response.CacheControl = "no-cache";

        Response.Write(result);

        //Response.Flush();
        //Response.End();
    }
    #endregion

    /// <summary>
    /// DataTable2Json
    /// </summary>
    /// <param name="dt"></param>
    /// <returns></returns>
    #region string DataTable2Json(DataTable dt)
    public string DataTable2Json(DataTable dt)
    {
        StringBuilder jsonBuilder = new StringBuilder();
        jsonBuilder.Append("[");
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            jsonBuilder.Append("{");
            for (int j = 0; j < dt.Columns.Count; j++)
            {
                jsonBuilder.Append("\"");
                jsonBuilder.Append(dt.Columns[j].ColumnName);
                jsonBuilder.Append("\":\"");
                jsonBuilder.Append(dt.Rows[i][j].ToString());
                jsonBuilder.Append("\",");
            }
            jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
            jsonBuilder.Append("},");
        }
        jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
        jsonBuilder.Append("]");
        return jsonBuilder.ToString();
    }
    #endregion


    /// <summary>
    /// DataTable2Arry
    /// </summary>
    /// <param name="dt"></param>
    /// <returns></returns>
    #region string DataTable2Arry(DataTable dt)
    public string DataTable2Arry(DataTable dt)
    {
        StringBuilder jsonBuilder = new StringBuilder();
        jsonBuilder.Append("{\"");
        jsonBuilder.Append(dt.TableName);
        jsonBuilder.Append("\":[");
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            jsonBuilder.Append("{");
            for (int j = 0; j < dt.Columns.Count; j++)
            {
                jsonBuilder.Append("\"");
                jsonBuilder.Append(dt.Columns[j].ColumnName);
                jsonBuilder.Append("\":\"");
                jsonBuilder.Append(dt.Rows[i][j].ToString());
                jsonBuilder.Append("\",");
            }
            jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
            jsonBuilder.Append("},");
        }
        jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
        jsonBuilder.Append("]");
        jsonBuilder.Append("}");
        return jsonBuilder.ToString();
    }
    #endregion
}
